package org.chenbingkang.jd;

import com.alibaba.fastjson.JSONObject;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

public class PushToFileApplication {
    public static void main(String... args) throws Exception {
        String configPath = args[0];
        Properties properties = new Properties();
        properties.load(new FileInputStream(configPath));
        System.out.println(properties);
        RedisClient redisClient = RedisClient.create(properties.getProperty("redis"));
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        RedisCommands<String, String> syncCommands = connection.sync();
        String dir = properties.getProperty("dir");
        FileWriter fileWriter = new FileWriter(dir + "/" + UUID.randomUUID().toString());
        int count = 0;
        try {
            count = Integer.parseInt(properties.getProperty("count"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        if (count == 0) {
            count = 100000;
        }
        for (int i = 0; i < count; i++) {
            if (i % 1000 == 0) {
                System.out.println(i);
                fileWriter.flush();
            }
            String s = syncCommands.lpop(properties.getProperty("key"));
            if (s == null || s.length() == 0 || "null".equals(s)) {
                break;
            }
            Map<String, Object> map = JSONObject.parseObject(s, Map.class);
          //  reqId
            /**
             *         RptPullResponseBean rptPullResponseBean=new RptPullResponseBean();
             *         rptPullResponseBean.setCode(m.getRptCode());
             *         rptPullResponseBean.setOrderId(String.valueOf(m.getOrderId()));
             *         rptPullResponseBean.setPhone(m.getPhone());
             *         String receiveTime=m.getReceiveTime();
             *         StringBuilder sb=new StringBuilder();
             *         sb.append(receiveTime.substring(0,4)).append("-").append(receiveTime.substring(4,6)).append("-")
             *                 .append(receiveTime.substring(6,8)).append(" ").append(receiveTime.substring(8,10)).append(":")
             *                 .append(receiveTime.substring(10,12)).append(":").append(receiveTime.substring(12,14));
             *         rptPullResponseBean.setReceiveTime(sb.toString());
             *         rptPullResponseBean.setReqId(m.getReqid());
             *         String sendTime =m.getSendTime();
             *         sb=new StringBuilder();
             *         sb.append(sendTime.substring(0,4)).append("-").append(sendTime.substring(4,6)).append("-")
             *                 .append(sendTime.substring(6,8)).append(" ").append(sendTime.substring(8,10)).append(":")
             *                 .append(sendTime.substring(10,12)).append(":").append(sendTime.substring(12,14));
             *         rptPullResponseBean.setSendTime(sb.toString());
             *         rptPullResponseBean.setState(m.getRptState());
             */
            String code=String.valueOf(map.get("rpt_code"));
            String orderId=String.valueOf(map.get("order_id").toString());
            String phone=String.valueOf(map.get("phone").toString());
            String receiveTime=String.valueOf(map.get("receive_time").toString());
            String reqId=String.valueOf(map.get("reqid").toString());
            String sendTime=String.valueOf(map.get("send_time").toString());
            String state=String.valueOf(map.get("rpt_state").toString());

            fileWriter.write(code+"|"+orderId+"|"+phone+"|"+receiveTime+"|"+reqId+"|"+sendTime+"|"+state);
            fileWriter.write("\n");
        }
        fileWriter.flush();
        fileWriter.close();

    }
}
